package com.hu.bgp.server;

import java.io.File;
import java.util.Hashtable;

import javax.servlet.ServletContext;

import com.hu.bgp.framework.ServerFacade;

import com.hu.bgp.framework.clientapi.ClientToServerMessage;
import com.hu.bgp.framework.clientapi.GameID;

/**
 * Deze singleton beheert alle gametypes, games, enzovoort.
 * 
 * @author ejvos
 */
public class GameServerContext
{
	private static final String CONTEXT_KEY = "GameServer";

	public static GameServerContext getInstance(ServletContext servletContext)
	{
		Object object = servletContext.getAttribute(CONTEXT_KEY);
		GameServerContext instance = null;

		if (object == null || !(object instanceof GameServerContext)) {
			instance = new GameServerContext(servletContext);
			servletContext.setAttribute(CONTEXT_KEY, instance);
		} else {
			instance = (GameServerContext) object;
		}
		return instance;
	}

	private GameTypeCollection gameTypeCollection = new GameTypeCollection();
	private BgpConfiguration configuration;
	private BarLoader barLoader;
	private BarFileSystemWatcher bfsWatcher;
	private String playerClientModulePath;

	private GameServerContext(ServletContext servletContext) {
		configuration = new BgpConfiguration(servletContext);
		bfsWatcher = new BarFileSystemWatcher(this);
		barLoader = new BarLoader(this);
		bfsWatcher.start();
		playerClientModulePath = servletContext.getRealPath("/playerclient");
	}

	public GameTypeCollection getGameTypeCollection()
	{
		return gameTypeCollection;
	}
	// selected for deletion
	@Deprecated
	public GameType getGameTypeByAlias(String gameAlias)
	{
		return gameTypeCollection.getByAlias(gameAlias);
	}

	public BgpConfiguration getConfiguration()
	{
		return configuration;
	}

	public BarLoader getBarLoader()
	{
		return barLoader;
	}

	public ServerFacade getGameInstance(GameID gameID)
	{
		return gameTypeCollection.getByAlias(gameID.getGameAlias())
		.getGameById(gameID.getGameID());
	}

	public ServerFacade initGameInstance(String creatorID, String alias)
	{
		GameType game = gameTypeCollection.getByAlias(alias);
		return game.initGameInstance();
	}

	public void dispatchMessageToGameInstance(ClientToServerMessage message)
	{
		getGameInstance(message.getGameID()).handleMessage(message);
	}

	public File getResourcesFileByContextPath(String contextPath)
	{
		String resourcesUrlPrefix = configuration.getResourcesUrlPrefix();
		String playerClientUrlPrefix = configuration.getPlayerClientUrlPrefix();
		File file = null;
		if (contextPath.startsWith(resourcesUrlPrefix)) {
			String path = contextPath.substring(resourcesUrlPrefix.length());

			int firstSlash = path.indexOf('/');
			String alias = path.substring(0, firstSlash);
			String resourcePath = path.substring(firstSlash + 1);

			GameType gt = gameTypeCollection.getByAlias(alias);
			file = new File(gt.getBarExtractDir() + File.separator
					+ configuration.getBarResourceDirName() + File.separator
					+ resourcePath);
		} else if (contextPath.startsWith(playerClientUrlPrefix)) {
			String path = contextPath.substring(playerClientUrlPrefix.length());
			file = new File(playerClientModulePath + File.separator + path);
		}
		return file;
	}

}
